<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/database.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/profile.php';

	//Ensure it's the logged in user
	if ($user['id'] != $_SESSION['user']) {
		header("Location:http://" . $_SERVER['HTTP_HOST'] . '/deviantpic/index.php');
		die();
	}

	//Initialize variable
	$thread		= -1;
	$receiver	= "";
	$subject	= "";

	//Is it replying?
	if (isset($_GET['reply'])) {
		//Get message
		$result = mysql_query("SELECT * FROM messages WHERE id=" . $_GET['reply']);
		$reply	= mysql_fetch_array($result);

		//Set data
		$thread		= $reply['thread'];
		$sender_id	= $reply['sender_id'];
		$subject	= "RE: " . $reply['subject'];

		//Get sender name
		$result		= mysql_query("SELECT username FROM users WHERE id=" . $sender_id);
		$row		= mysql_fetch_array($result);
		$receiver	= $row['username'];
	}

	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/doctype.php';
?>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>deviantPIC - New Message</title>
    </head>
    <body>
		<!-- Header -->
		<?php require $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/header.php'; ?>

		<!-- Content -->
		<div id="body">

			<!-- Message -->
			<div id="message-form">
				<form method="post" action=<?php echo "'index.php?id=" . $user['id'] . "'" ?>> <p>
				<!-- Receiver input -->
				<label for="input_receiver" >Send to: </label>
				<input type="text"
					   id="input_receiver"
					   name="receiver"
					   value=<?php echo '"' . $receiver . '"' ?>
				/> <span id="result_receiver"></span> <br />

				<!-- Subject input -->
				<label for="input_subject" >Subject: </label>
				<input type="text"
					   id="input_subject"
					   name="subject"
					   value=<?php echo '"' . $subject . '"' ?>
				/> <span id="result_subject"></span> <br />

				<!-- Message input -->
				<label for="input_message" >Message: </label>
				<textarea cols="80" rows="6" id="input_message" name="message"></textarea>
				<br />

				<!-- Thread -->
				<input type="hidden" name="thread" value=<?php echo '"' . $thread . '"' ?> />

				<!-- Button -->
				<input type="submit" value="Send" />
			</p></form>
			</div>
		</div>

		<!-- Footer -->
		<?php require $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/footer.php'; ?>
    </body>
</html>